package VIEWS;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import ModelEntityFW.BaoHiemEntity;
import ModelEntityFW.UserEntitys;
import Ultils.DateConverter;

import javax.swing.JToolBar;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import com.toedter.calendar.JDateChooser;

import CONTROLLERS.BaoHiemControllers;

import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

public class frmEditAddBaoHiem extends JFrame {

	private JButton button;
	private JPanel contentPane;
	private JTextField soThe;
	private JTextField hoVaTen;
	private JTextField diaChi;
	private JTextField noiDK;
	private final ButtonGroup btgGender = new ButtonGroup();
	private JDateChooser ngaySinh;
	private JRadioButton rbNam;
	private JRadioButton rbNu;
	private JDateChooser ngayDK;
	private JDateChooser ngayHetHan;
	private static Connection m_objConnections;
	private BaoHiemControllers m_objControllers;
	private static UserEntitys m_objSecurityContext;
	private static BaoHiemEntity m_objBaoHiemEntity;
	private String strGender;
	
	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frmEditAddBaoHiem frame = new frmEditAddBaoHiem();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}*/

	/**
	 * Create the frame.
	 */
	public frmEditAddBaoHiem(Connection objConnection, UserEntitys objSecurity, BaoHiemEntity objBaoHiemEntity) {
		m_objConnections = objConnection;
		m_objSecurityContext = objSecurity;
		m_objBaoHiemEntity = objBaoHiemEntity;
		m_objControllers = new BaoHiemControllers(m_objConnections, m_objSecurityContext);
		
		
		soThe = new JTextField();
		button = new JButton("New Button");
		soThe.setEditable(false);
		if(m_objBaoHiemEntity.isbFlag()){
			setTitle("Chỉnh sửa thông tin bảo hiểm");
			button.setText("Cập nhật");
		}else{
			setTitle("Thêm bảo hiểm mới");
			button.setText("Thêm mới");
		}
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 416, 449);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JToolBar toolBar = new JToolBar();
		toolBar.setBounds(0, 0, 400, 50);
		contentPane.add(toolBar);
		
		
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				m_objBaoHiemEntity.setSoThe(soThe.getText());
				m_objBaoHiemEntity.setHoVaTen(hoVaTen.getText());
				m_objBaoHiemEntity.setNgaySinh(DateConverter.convertDateToString(ngaySinh.getDate(), "yyyy-MM-dd"));
				m_objBaoHiemEntity.setGioiTinh(strGender);
				m_objBaoHiemEntity.setDiaChi(diaChi.getText());
				m_objBaoHiemEntity.setNoiDKKCB(noiDK.getText());
				m_objBaoHiemEntity.setNgayDangKi(DateConverter.convertDateToString(ngayDK.getDate(), "yyyy-MM-dd"));
				m_objBaoHiemEntity.setNgayHetHan(DateConverter.convertDateToString(ngayHetHan.getDate(), "yyyy-MM-dd"));
				if(m_objBaoHiemEntity.isbFlag()){
					boolean bResult=false;
					try{
						bResult=m_objControllers.ExecOfUpdateElement(m_objBaoHiemEntity);
					}catch(Exception ex){
						JOptionPane.showMessageDialog(getParent(), "Lỗi: " + ex.getMessage(), "ERROR!!!",
								JOptionPane.ERROR_MESSAGE);
					}
					
					if(bResult){
						JOptionPane.showMessageDialog(getParent(), "Cập nhật thành công!!!", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
					}
					m_objBaoHiemEntity.setbFlag(false);
					dispose();
				}else{
					boolean bResult=false;
					try{
						bResult=m_objControllers.ExecOfInsertElement(m_objBaoHiemEntity);
					}catch(Exception ex){
						JOptionPane.showMessageDialog(getParent(), "Lỗi: " + ex.getMessage(), "ERROR!!!",
								JOptionPane.ERROR_MESSAGE);
					}
					
					if(bResult){
						JOptionPane.showMessageDialog(getParent(), "Thêm thông tin thành công!!!", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
					}
					m_objBaoHiemEntity.setbFlag(false);
					dispose();
				}
			}
		});
		button.setIcon(new ImageIcon(frmEditAddBaoHiem.class.getResource("/images/13.png")));
		button.setFont(new Font("Cambria", Font.PLAIN, 14));
		toolBar.add(button);
		
		JButton btnThotx = new JButton("Đóng");
		btnThotx.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btnThotx.setIcon(new ImageIcon(frmEditAddBaoHiem.class.getResource("/images/6_3.png")));
		btnThotx.setFont(new Font("Cambria", Font.PLAIN, 14));
		toolBar.add(btnThotx);
		
		JLabel lblNewLabel = new JLabel("Số thẻ");
		lblNewLabel.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblNewLabel.setBounds(20, 70, 85, 18);
		contentPane.add(lblNewLabel);
		
		JLabel lblHVTn = new JLabel("Họ và tên");
		lblHVTn.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblHVTn.setBounds(20, 110, 85, 18);
		contentPane.add(lblHVTn);
		
		JLabel lblNgySinh = new JLabel("Ngày sinh");
		lblNgySinh.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblNgySinh.setBounds(20, 150, 85, 18);
		contentPane.add(lblNgySinh);
		
		JLabel lblGiiTnh = new JLabel("Giới tính");
		lblGiiTnh.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblGiiTnh.setBounds(20, 190, 85, 18);
		contentPane.add(lblGiiTnh);
		
		JLabel lblaCh = new JLabel("Địa chỉ");
		lblaCh.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblaCh.setBounds(20, 230, 85, 18);
		contentPane.add(lblaCh);
		
		JLabel lblNikkcb = new JLabel("Nơi ĐKKCB");
		lblNikkcb.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblNikkcb.setBounds(20, 270, 85, 18);
		contentPane.add(lblNikkcb);
		
		JLabel lblNgyngK = new JLabel("Ngày đăng ký");
		lblNgyngK.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblNgyngK.setBounds(20, 310, 85, 18);
		contentPane.add(lblNgyngK);
		
		JLabel lblNgyHtHanj = new JLabel("Ngày hết hạn");
		lblNgyHtHanj.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblNgyHtHanj.setBounds(20, 350, 85, 18);
		contentPane.add(lblNgyHtHanj);
		
		
		soThe.setFont(new Font("Cambria", Font.PLAIN, 14));
		soThe.setBounds(134, 65, 240, 26);
		contentPane.add(soThe);
		soThe.setColumns(10);
		
		hoVaTen = new JTextField();
		hoVaTen.setFont(new Font("Cambria", Font.PLAIN, 14));
		hoVaTen.setColumns(10);
		hoVaTen.setBounds(134, 105, 240, 26);
		contentPane.add(hoVaTen);
		
		diaChi = new JTextField();
		diaChi.setFont(new Font("Cambria", Font.PLAIN, 14));
		diaChi.setColumns(10);
		diaChi.setBounds(134, 225, 240, 26);
		contentPane.add(diaChi);
		
		noiDK = new JTextField();
		noiDK.setFont(new Font("Cambria", Font.PLAIN, 14));
		noiDK.setColumns(10);
		noiDK.setBounds(134, 265, 240, 26);
		contentPane.add(noiDK);
		
		ngaySinh = new JDateChooser();
		ngaySinh.setDateFormatString("dd/MM/yyyy");
		ngaySinh.setBounds(134, 145, 147, 26);
		contentPane.add(ngaySinh);
		
		rbNam = new JRadioButton("Nam");
		rbNam.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				strGender = rbNam.getLabel();
			}
		});
		btgGender.add(rbNam);
		rbNam.setFont(new Font("Cambria", Font.PLAIN, 14));
		rbNam.setBounds(134, 185, 78, 26);
		contentPane.add(rbNam);
		
		rbNu = new JRadioButton("Nữ");
		rbNu.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				strGender = rbNu.getLabel();
			}
		});
		btgGender.add(rbNu);
		rbNu.setFont(new Font("Cambria", Font.PLAIN, 14));
		rbNu.setBounds(214, 185, 67, 26);
		contentPane.add(rbNu);
		
		ngayDK = new JDateChooser();
		ngayDK.setDateFormatString("dd/MM/yyyy");
		ngayDK.setBounds(134, 305, 147, 26);
		contentPane.add(ngayDK);
		
		ngayHetHan = new JDateChooser();
		ngayHetHan.setDateFormatString("dd/MM/yyyy");
		ngayHetHan.setBounds(134, 345, 147, 26);
		contentPane.add(ngayHetHan);
		BindingControls(m_objBaoHiemEntity);
	}
	
	public void BindingControls(BaoHiemEntity objBaoHiemEntity){
		if(objBaoHiemEntity.isbFlag()){
			soThe.setText(objBaoHiemEntity.getSoThe());
			hoVaTen.setText(objBaoHiemEntity.getHoVaTen());
			ngaySinh.setDate(DateConverter.convertStringToDate(objBaoHiemEntity.getNgaySinh(), "yyyy-MM-dd"));
			if(objBaoHiemEntity.getGioiTinh().equals("Nam")){
				rbNam.setSelected(true);
			}else
				rbNu.setSelected(true);
			diaChi.setText(objBaoHiemEntity.getDiaChi());
			noiDK.setText(objBaoHiemEntity.getNoiDKKCB());
			ngayDK.setDate(DateConverter.convertStringToDate(objBaoHiemEntity.getNgayDangKi(), "yyyy-MM-dd"));
			ngayHetHan.setDate(DateConverter.convertStringToDate(objBaoHiemEntity.getNgayHetHan(), "yyyy-MM-dd"));
		}else{
			soThe.setText(objBaoHiemEntity.getSoThe());
		}
	}
}
